package com.fingard.dsp.bank.directbank.gycb01;

import java.util.Date;

import com.fingard.constant.DateFormat2;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class GYCB01SinglePay extends GYCB01Base implements ISinglePay {

    //单笔支付，不需采用流处理方式
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        if(p_req1908.reqHeader.commandCode.equals("1908_B2eAggregatePayment")){
            tmpRetDetail = d_1908_B2eAggregatePayment(p_req1908);
        }else {
            tmpRetDetail = d_190802(p_req1908);
        }
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    //190802对外支付
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        GYCB01Header bankHeader = new GYCB01Header();
        bankHeader.transName = "1".equals(p_req1908.singleDetail.sameBnk) ? "B2eTransferInBank4UTF8" : "B2eTransferOutBank4UTF8";
        bankHeader.batchID = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        XElement tmpXElement = new XElement("RequestRecord", "");
        tmpXElement.addChild(new XElement("AcctNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("PayeeAcctNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("PayeeAcctName", p_req1908.singleDetail.recName));
        if (p_req1908.singleDetail.sameBnk.equals("0")) {
            tmpXElement.addChild(new XElement("UnioBank", p_req1908.singleDetail.cnapsName));
            tmpXElement.addChild(new XElement("UnioNo", p_req1908.singleDetail.cnapsCode));
        }
        tmpXElement.addChild(new XElement("Amount", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("Note", p_req1908.singleDetail.purpose));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String respondCode = tmpBkReader.getSimpleText("respondCode");
                String respondInfo = tmpBkReader.getSimpleText("respondInfo");
                if (StringHelper.hasAnyChar(respondCode)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = respondInfo;
                    retDetail.transState = respondCode;
                    retDetail.payInfoCode = respondCode;
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = respondInfo;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //统一支付
    private RetAtsSglDetail d_1908_B2eAggregatePayment(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        GYCB01Header bankHeader = new GYCB01Header();
        bankHeader.transName = "B2eAggregatePayment";
        bankHeader.batchID = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        XElement tmpXElement = new XElement("RequestRecord", "");
        tmpXElement.addChild(new XElement("AcctNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("PayeeAcctNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("PayeeAcctName", p_req1908.singleDetail.recName));
        if (p_req1908.singleDetail.sameBnk.equals("0")) {
            tmpXElement.addChild(new XElement("UnioBank", p_req1908.singleDetail.cnapsName));
            tmpXElement.addChild(new XElement("UnioNo", p_req1908.singleDetail.cnapsCode));
        }
        tmpXElement.addChild(new XElement("Amount", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("Note", p_req1908.singleDetail.purpose));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String respondCode = tmpBkReader.getSimpleText("respondCode");
                String respondInfo = tmpBkReader.getSimpleText("respondInfo");
                if (StringHelper.hasAnyChar(respondCode)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = respondInfo;
                    retDetail.transState = respondCode;
                    retDetail.payInfoCode = respondCode;
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = respondInfo;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //1944 状态查询
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);

        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        GYCB01Header bankHeader = new GYCB01Header();
        bankHeader.transName = "B2eQryAgrgtePayResult4UTF8";
        bankHeader.batchID = getQryTransID();
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
        xmlWriter.writeStartElement("RequestRecord");
        xmlWriter.writeElementString("OriginalBatchID", p_req1944.askNumber);
        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String respondCode = tmpBkReader.getSimpleText("respondCode");
                    String respondInfo = tmpBkReader.getSimpleText("respondInfo");
                    if ("0000".equals(respondCode)) {
                        retDetail.respCode = "0";
                        retDetail.transState = respondCode + "+" + tmpBkReader.getFirstText("/transaction/RespondBody/RespondRecord/TransStatus");
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.respInfo = respondInfo;
                        retDetail.payInfoCode = retDetail.transState;
                    } else if ("RET200".equals(respondCode) || "RET300".equals(respondCode) || "RET400".equals(respondCode)) {
                        retDetail.respCode = "0";
                        retDetail.transState = "notexist";
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.respInfo = respondInfo;
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = respondInfo;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
